using System;
using System.Net;

namespace AdRotator.Networking
{
    internal class Network
    {
        public static void GetStringFromURL(Uri uri, Action<string, Exception> callback)
        {
            var c = new WebClient();
            //  c.AllowReadStreamBuffering = false;
            c.DownloadStringCompleted += (sender, e) =>
            {
                if (e.Error == null)
                    callback(e.Result, null);
                else
                    callback(null, e.Error);
            };

            c.DownloadStringAsync(uri);
        }

        internal static void GetDeviceIP(Action<string, Exception> callback)
        {
            var webClient = new WebClient();
            
            webClient.DownloadStringCompleted += (sender, e) =>
                {
                    if (e.Error == null)
                    {
                        var value = ((string)e.Result).Split(new Char[] { '"' });
                        callback(value[3], null);
                    }
                    else
                        callback(null, e.Error);
                };

            webClient.DownloadStringAsync(new Uri("http://compiledexperience.com/windows-phone-7/my-ip", UriKind.Absolute));
        }

    }
}
